package org.yaukie.builder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.quartz.Calendar;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.yaukie.util.DateTime;

public class QuartzManager {
    public static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    public QuartzManager() {
    }

    public static void addJob(Map param, Class<? extends Job> jobClass, String cron, Map<String, Object> parameter) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(param.get("jobName") + "", param.get("jobGroupName") + "").build();
            parameter.forEach((k, v) -> {
                jobDetail.getJobDataMap().put(k, v);
            });
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            triggerBuilder.withIdentity(param.get("triggerName") + "", param.get("triggerGroupName") + "");
            triggerBuilder.startNow();
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            CronTrigger trigger = (CronTrigger)triggerBuilder.build();
            sched.scheduleJob(jobDetail, trigger);
            if (!sched.isShutdown()) {
                sched.start();
            }

        } catch (Exception var8) {
            throw new RuntimeException(var8);
        }
    }

    public static void addOnceJob(Map param, Class<? extends Job> jobClass, Map<String, Object> parameter) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(param.get("jobName") + "", param.get("jobGroupName") + "").build();
            parameter.forEach((k, v) -> {
                jobDetail.getJobDataMap().put(k, v);
            });
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            triggerBuilder.withIdentity(param.get("triggerName") + "", param.get("triggerGroupName") + "");
            triggerBuilder.startNow();
            StringBuilder cronBuilder = new StringBuilder();
            DateTime dateTime = new DateTime();
            Integer addMinute = dateTime.second() >= 58 ? 2 : 1;
            cronBuilder.append("0").append(" ").append(dateTime.minute() + addMinute).append(" ").append(dateTime.hour(true)).append(" ").append(dateTime.dayOfMonth()).append(" ").append(dateTime.monthStartFromOne()).append(" ").append("?").append(" ").append(String.valueOf(dateTime.year()));
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronBuilder.toString()));
            CronTrigger trigger = (CronTrigger)triggerBuilder.build();
            sched.scheduleJob(jobDetail, trigger);
            sched.getListenerManager().addJobListener(new QuartzListener(), KeyMatcher.keyEquals(new JobKey(param.get("jobName") + "", param.get("jobGroupName") + "")));
            if (!sched.isShutdown()) {
                sched.start();
            }
        } catch (Exception var10) {
            var10.printStackTrace();
        }

    }

    public static void modifyJobTime(Map param, String cron) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(param.get("triggerName") + "", param.get("triggerGroupName") + "");
            CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey);
            if (trigger != null) {
                String oldTime = trigger.getCronExpression();
                if (!oldTime.equalsIgnoreCase(cron)) {
                    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
                    triggerBuilder.withIdentity(param.get("triggerName") + "", param.get("triggerGroupName") + "");
                    triggerBuilder.startNow();
                    triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
                    trigger = (CronTrigger)triggerBuilder.build();
                    sched.rescheduleJob(triggerKey, trigger);
                }

            }
        } catch (Exception var7) {
            throw new RuntimeException(var7);
        }
    }

    public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
            sched.pauseTrigger(triggerKey);
            sched.unscheduleJob(triggerKey);
            sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static void startJobs() {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            sched.start();
        } catch (Exception var1) {
            throw new RuntimeException(var1);
        }
    }

    public static void shutdownJobs() {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            if (!sched.isShutdown()) {
                sched.shutdown();
            }

        } catch (Exception var1) {
            throw new RuntimeException(var1);
        }
    }

    public static void main(String[] args) {
        new ArrayList();

        try {
            CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
            cronTriggerImpl.setCronExpression("1/59 1/1 * * * ? *");
            List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, (Calendar)null, 1);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator var5 = dates.iterator();

            while(var5.hasNext()) {
                Date date = (Date)var5.next();
                System.out.println("111111" + dateFormat.format(date));
            }
        } catch (ParseException var7) {
            var7.printStackTrace();
        }

    }
}
